package com.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.entity.ElcBlacklist;
import com.ssm.service.ElcBlacklistService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.groups.Default;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (ElcBlacklist)表控制层
 *
 * @author makejava
 * @since 2021-06-01 10:37:07
 */
@RestController
@RequestMapping("elcBlacklist")
public class ElcBlacklistController {
    /**
     * 服务对象
     */
    @Resource
    private ElcBlacklistService elcBlacklistService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOneElcBlack")
    public ElcBlacklist selectOne(Integer id) {
        return this.elcBlacklistService.queryById(id);
    }


    @RequestMapping("listElcBlack")
    public PageInfo<ElcBlacklist> queryAll(ElcBlacklist elcBlacklist, Integer rows, Integer page) {
        //分页组件
        PageHelper.startPage(page, rows);
        if (elcBlacklist.getDetail() != null) {
            elcBlacklist.setDetail("%" + elcBlacklist.getDetail() + "%");
        }
        //查询所有黑名单信息
        List<ElcBlacklist> list = elcBlacklistService.queryAll(elcBlacklist);
        PageInfo<ElcBlacklist> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @RequestMapping("queryAllN")
    public List<String> queryAllN() {
        //查询所有黑名单信息
        List<ElcBlacklist> elcBlacklists = elcBlacklistService.queryAllN();
        List<String> list = new ArrayList<>();
        for (ElcBlacklist a : elcBlacklists) {
            list.add(a.getDetail());
        }
        return list;
    }


    //黑名单增加操作
    @RequestMapping("addElcBlack")
    public String addElcBlack(ElcBlacklist elcBlacklist) {
        if (checkBlack(elcBlacklist.getDetail())=="null") {
            return "null";
        }
        if (checkBlack(elcBlacklist.getDetail()).equals("false")) {
            return "like";
        }
        elcBlacklist.setDpid(0);
        elcBlacklist.setCreateTime(new Date());
        elcBlacklist.setUpdateTime(new Date());
        elcBlacklistService.insert(elcBlacklist);
        return "success";
    }

    public String checkBlack(@RequestParam(defaultValue = "") String detail) {
        if (detail == "") {
            return "null";
        }
        return elcBlacklistService.queryByDetail(detail) + "";
    }


    //通过ID查询单条黑名单数据
    @RequestMapping("queryElcBlack/{id}")
    public ElcBlacklist queryElcBlackById(@PathVariable Integer id) {
        ElcBlacklist elcBlacklist = elcBlacklistService.queryById(id);
        return elcBlacklist;
    }


    //修改黑名单数据信息
    @RequestMapping("updateElcBlack")
    public String updateElcBlack(ElcBlacklist elcBlacklist) {

        String detail = elcBlacklistService.queryById(elcBlacklist.getId()).getDetail();
        if (checkBlack(elcBlacklist.getDetail())=="null") {
            return "null";
        }
        if (checkBlack(elcBlacklist.getDetail()).equals("false") && !elcBlacklist.getDetail().equals(detail)) {
            return "like";
        }
        elcBlacklist.setUpdateTime(new Date());
        elcBlacklistService.update(elcBlacklist);
        return "success";
    }

    //删除黑名单数据信息
    @RequestMapping("delElcBlack/{id}")
    public String delElcBlack(@PathVariable Integer id) {
        elcBlacklistService.deleteById(id);
        return "success";
    }

    @RequestMapping("delElcBlacks/{ids}")
    public String delElcBlacks(@PathVariable Integer[] ids) {
        elcBlacklistService.deleteByIds(ids);
        return "success";
    }

    @RequestMapping("queryAll")
    public List<ElcBlacklist> queryAll(ElcBlacklist elcBlacklist) {
        return elcBlacklistService.queryAll(elcBlacklist);
    }
}
